package com.vf.cloud.scene.mvc.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.common.datasource.annotation.Tx;
import com.vf.cloud.common.util.R;
import com.vf.cloud.common.util.UuidUtil;
import com.vf.cloud.scene.common.domain.SubLayerElement;
import com.vf.cloud.scene.common.domain.SubPoi;

@RestController
@RequestMapping("/scene/biz/poi")
public class PoiController {

//	@Autowired
//	private ILayerService layerService;

	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<SubPoi>> findList(HttpServletRequest request,
			@RequestParam(name = "layerId", required = false, defaultValue = "") String layerId,
			@RequestParam(name = "projectId", required = false, defaultValue = "") String projectId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + SubPoi.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.CN_TEXT LIKE '%" + keywords + "%' OR O.EN_TEXT LIKE '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(layerId)) {
			sqlExceptSelect += "and o.id in ( select e.element_id FROM sub_layer_element e ,sub_layer l WHERE e.layer_id=l.id and e.layer_id='"
					+ layerId + "' )";
		}
		if (!StrKit.isBlank(projectId)) {
			sqlExceptSelect += "and o.project_id = '"+projectId+"' ";
		}
		Page<SubPoi> pages = SubPoi.dao.paginate(cur, limit, "select * ", sqlExceptSelect);
		return R.ok(pages);
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SubPoi> save(@RequestParam(name = "poi", required = false) String poi,
			@RequestParam(name = "layerIds", required = false) String layerIds) {
		try {

			if (StrKit.isBlank(poi)) {
				return R.failed("参数必填!");
			}
			SubPoi p = JSONObject.parseObject(poi, SubPoi.class);
			if (p == null) {
				return R.failed("参数必填!");
			}
			JSONArray ids = null;
			if (!StrKit.isBlank(layerIds)) {
				ids = JSONArray.parseArray(layerIds);
			}

			if (StrKit.isBlank(p.getCnText())) {
				return R.failed("名称不可以为空！");
			}

			if (StrKit.notBlank(p.getId())) {
				if (p.update()) {
					if (ids != null) {
						Db.use().delete("delete from " + SubLayerElement.TABLE_NAME + " where element_id=? ", p.getId());
						for (Object object : ids) {
							String layerId = (String) object;
							SubLayerElement layerElement = new SubLayerElement();
							layerElement.setLayerId(layerId);
							layerElement.setElementId(p.getId());
							layerElement.save();
						}
					}
					return R.ok(p);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				p.setId(UuidUtil.getUUID());
				if (p.save()) {
					if (ids != null) {
						Db.use().delete("delete from " + SubLayerElement.TABLE_NAME + " where element_id=? ", p.getId());
						for (Object object : ids) {
							String layerId = (String) object;
							SubLayerElement layerElement = new SubLayerElement();
							layerElement.setLayerId(layerId);
							layerElement.setElementId(p.getId());
							layerElement.save();
						}
					}
					return R.ok(p);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/saveElementLayer", method = RequestMethod.POST)
	public R<Object> saveElementLayer(@RequestParam(name = "elementIds", required = false) String elementIds,
			@RequestParam(name = "layerId", required = false) String layerId) {
		try {

			if (StrKit.isBlank(elementIds)) {
				return R.failed("参数必填!");
			}
			
			if (StrKit.isBlank(layerId)) {
				return R.failed("参数必填!");
			}
			
			JSONArray ids = JSONArray.parseArray(elementIds);
			if(ids==null || ids.size()==0) {
				return R.failed("参数必填!");
			}
			
			for (Object object : ids) {
				String elementId = (String) object;
				SubLayerElement layerElement = SubLayerElement.dao.findFirst(
						"select * from "+SubLayerElement.TABLE_NAME+" where layer_id=? and element_id=? ",layerId,elementId);
				if(layerElement == null) {
					layerElement = new SubLayerElement();
					layerElement.setLayerId(layerId);
					layerElement.setElementId(elementId);
					layerElement.save();
				}
			}
			return R.ok("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/deleteById", method = RequestMethod.GET)
	public R<String> deleteById(@RequestParam(name = "id") String id) throws Exception {
		try {
			SubPoi poi = SubPoi.dao.findById(id);
			if (poi == null)
				return R.failed("数据不存在，刷新后尝试!");
			if (poi.delete()) {
				Db.use().delete("delete from " + SubLayerElement.TABLE_NAME + " where element_id=? ", poi.getId());
				return R.ok();
			} else {
				return R.failed("失败！");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/getPoiLayer", method = RequestMethod.GET)
	public R<Object> getPoiLayer(@RequestParam(name = "id") String id) throws Exception {
		try {
			List<SubLayerElement> layers = SubLayerElement.dao
					.find("select * from " + SubLayerElement.TABLE_NAME + " where element_id=? ", id);
			if (layers == null)
				return R.failed();
			return R.ok(layers);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

}
